package comoabejas

import java.util.HashMap;
import java.util.List;

/**
 * 
 * @author matias
 *
 */
class UserService {//implements IUserService{

    static transactional = true

    def serviceMethod() {

    }
    
    public User saveObj(User u){
    	if(u.save(flush: true)){
    		return u;
    	}
    }
    
    def listObj(){
       def g = [instanceList: User.list(), instanceTotal: User.count()]
       return g;
    }
    public User getObj(Long id){
        def instance = Imagen.get(id)
        return instance
    }
    public void deleteObj(User obj){
        obj.delete(flush: true)

    }
    
    public User register(User u){
    }
	
    public User login(User u){
		//def user = User.findWhere(email:u.getEmail(),password:u.getPassword())
		def user = User.find("from User as u where u.email = :em and u.password= :pw",[em:u.getEmail(),pw:u.getPassword()])
		//Deal.findAll("from Deal as b where b.state = :state order by b.endDate desc",[state:state])

		return user
    }
	
    public void buyDeal(){
    	
    }
    public void listPastDeals(){
    	
    }
    public void subscribeNewsleter(){
    	
    }
    
	def generateData = {
		
		List<User> listaUsers = new ArrayList<User>()
		listaUsers =  User.list()
		
		for (User a : listaUsers) {
			a.delete(flush : true)
		}

		User user = new User()
		user.setName("Matias")
		user.setEmail("matiaskochman1@gmail.com")
		user.setAddress("address 1")
		user.setUsername("matiaskochman");
		user.setPassword("pepe1")
		user.setMiddleName("Eduardo");
		user.setSurname("Kochman");
		user.setPhone("42493157");
	
		listaUsers.add(0, user)

		user = new User()
		user.setName("Luciano")
		user.setEmail("lucianolacanna1@gmail.com")
		user.setAddress("address 2")
		user.setUsername("lucianolacanna");
		user.setPassword("pepe2")
		user.setMiddleName("ricardo");
		user.setSurname("Lacanna");
		user.setPhone("42407381");

		listaUsers.add(1, user)
		
		try{
			for (User a : listaUsers) {
				a.save(flush :true)
			}
			
		}catch (Exception e) {
			e.printStackTrace()
		}
	}

	
}
